package com.qing.demo.netty.service.impl;

import cn.hutool.json.JSONUtil;
import com.qing.demo.netty.domain.User;
import com.qing.demo.netty.service.UserRepository;
import com.qing.demo.netty.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.stereotype.Service;

/**
 * @author caozhipeng
 * @date 2025/8/2
 */
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private UserRepository userRepository;

    @Override
    public void save(User user) {
        userRepository.save(user);
        System.out.println(JSONUtil.toJsonStr( user));
    }

    @Override
    public void deleteById(String id) {
        userRepository.deleteById( id);
    }

    @Override
    public User findById(String id) {
        return userRepository.findById(id).orElse(null);
    }

    @Override
    public Iterable<User> queryAll() {
        return userRepository.findAll();
    }

    @Override
    public Page<User> findByName(String name, int page, int size) {
        return userRepository.findByNameLike(name, PageRequest.of(page, size));
    }
}
